package com.dayu.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayu.finecomm.pojo.DeviceConfig;
import com.dayu.web.param.DeviceId;
import com.dayu.web.param.DeviceParam;
import com.dayu.web.param.TokenParam;
import com.dayu.web.service.DeviceService;
import com.dayu.web.utils.YamlUtil;
import com.dayu.web.vo.RESTResponse;

/**
 * 关于网关的增删改查
 * 
 * @author Chenjiawei
 *
 *         2017年5月31日
 */
@Controller
@RequestMapping("/unitalk/v1.0")
public class DeviceCtrl {

	static Logger LOGGER = LoggerFactory.getLogger(DeviceCtrl.class);

	@Resource
	DeviceService deviceService;

	/**
	 * 获取运行状态
	 * 
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/device/runstate.json", method = RequestMethod.POST)
	public String getDevicesState(@RequestBody TokenParam token) {

		if (YamlUtil.isExist()) {

			return RESTResponse.of(deviceService.getDevicesState()).toString();
		} else {
			return RESTResponse.of(1, "获取网关状态失败!").toString();
		}

	}

	/**
	 * 获取网关列表
	 * 
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/devices.json", method = RequestMethod.POST)
	public String getDevices(@RequestBody TokenParam token) {

		if (YamlUtil.isExist()) {

			return RESTResponse.of(YamlUtil.getDevices()).toString();
		} else {
			return RESTResponse.of(1, "获取网关失败!").toString();
		}
	}

	/**
	 * 新增网关
	 * 
	 * @param deviceParam
	 *            网关
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/device/add.json", method = RequestMethod.POST)
	public String addDevice(@RequestBody DeviceParam deviceParam) {

		if (YamlUtil.isExist()) {

			List<DeviceConfig> list = deviceService.addDevice(deviceParam);

			Map<String, List<DeviceConfig>> map = new HashMap<String, List<DeviceConfig>>();
			map.put("finecomms", list);

			YamlUtil.setProper(map);

			return RESTResponse.of(0, "新增网关成功!").toString();
		} else {
			return RESTResponse.of(1, "新增网关失败!").toString();
		}

	}

	/**
	 * 修改网关设备
	 * 
	 * @param deviceParam
	 *            修改参数
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/device/update.json", method = RequestMethod.POST)
	public String modifyDevices(@RequestBody DeviceParam deviceParam) {

		if (YamlUtil.isExist()) {

			List<DeviceConfig> list = deviceService.updateDevices(deviceParam);

			Map<String, List<DeviceConfig>> map = new HashMap<String, List<DeviceConfig>>();
			map.put("finecomms", list);

			YamlUtil.setProper(map);

			return RESTResponse.of(0, "修改网关成功!").toString();
		} else {
			return RESTResponse.of(1, "修改网关失败!").toString();
		}
	}

	/**
	 * 删除网关设备
	 * 
	 * @param deviceId
	 *            网关标志
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/device/delete.json", method = RequestMethod.POST)
	public String deleteDevices(@RequestBody DeviceId deviceId) {

		if (YamlUtil.isExist()) {

			List<DeviceConfig> list = deviceService.delDevice(deviceId);

			Map<String, List<DeviceConfig>> map = new HashMap<String, List<DeviceConfig>>();
			map.put("finecomms", list);

			YamlUtil.setProper(map);

			return RESTResponse.of(0, "删除网关成功!").toString();
		} else {
			return RESTResponse.of(1, "删除网关失败!").toString();
		}

	}

}
